package com.baizhi.service;

import com.baizhi.bean.Perms;
import com.baizhi.bean.Role;
import com.baizhi.bean.User;
import com.baizhi.dao.UserDao;
import com.baizhi.util.SaltUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Override
    public void register(User user) {
        //处理业务调用Dao
        //生成随机盐
        String salt = SaltUtil.getSalt(8);
        //将随机盐保存到数据库
        user.setSalt(salt);
        //对明文的密码进行MD5和salt和hash散列
        Md5Hash md5Hash = new Md5Hash(user.getPassword(),salt,1024);
        user.setPassword(md5Hash.toHex());
        userDao.save(user);

    }

    @Override
    public User findRoleByUserName(String username) {
        return userDao.findRoleByUserName(username);
    }

    @Override
    public List<Perms> findPermsById(Integer id) {
        return userDao.findPermsById(id);
    }

    @Override
    public User findByUserName(String username) {
        return userDao.findByUserName(username);
    }
}
